//@version=5
indicator("Vip All", overlay=true)

// Input Parameters
lookbackPeriod = input.int(20, "Lookback Period for Key Levels")
atrPeriod = input.int(14, "ATR Period")
atrMultiplierSL = input.float(1.5, "SL ATR Multiplier")
atrMultiplierTP1 = input.float(1.5, "TP1 ATR Multiplier")
atrMultiplierTP2 = input.float(2.0, "TP2 ATR Multiplier")
rewardToRisk = input.float(2.0, "Reward to Risk Ratio")

// ATR and Volume Calculation
atr = ta.atr(atrPeriod)
volumeSMA = ta.sma(volume, atrPeriod)

// Key Levels Identification (Support & Resistance Zones)
support = ta.lowest(low, lookbackPeriod)
resistance = ta.highest(high, lookbackPeriod)
supportBuffer = support - atr * 0.5
resistanceBuffer = resistance + atr * 0.5

// Define Bullish and Bearish Scenario Entry Ranges (Visualized with Boxes)
var box bullishBox = na
var box bearishBox = na

// Bullish Scenario
isBullishEntry = (close > supportBuffer) and (low <= support) and (volume > volumeSMA)
if isBullishEntry
    if na(bullishBox)
        bullishBox := box.new(left=bar_index - 10, top=support + atr * 0.5, right=bar_index + 10, bottom=support - atr * 0.5, border_color=color.green, bgcolor=color.new(color.green, 85))
    else
        box.set_left(bullishBox, bar_index - 10)
        box.set_right(bullishBox, bar_index + 10)
        box.set_top(bullishBox, support + atr * 0.5)
        box.set_bottom(bullishBox, support - atr * 0.5)

// Bearish Scenario
isBearishEntry = (close < resistanceBuffer) and (high >= resistance) and (volume > volumeSMA)
if isBearishEntry
    if na(bearishBox)
        bearishBox := box.new(left=bar_index - 10, top=resistance + atr * 0.5, right=bar_index + 10, bottom=resistance - atr * 0.5, border_color=color.red, bgcolor=color.new(color.red, 85))
    else
        box.set_left(bearishBox, bar_index - 10)
        box.set_right(bearishBox, bar_index + 10)
        box.set_top(bearishBox, resistance + atr * 0.5)
        box.set_bottom(bearishBox, resistance - atr * 0.5)

// Stop Loss and Take Profit Calculations for Bullish and Bearish Scenarios
bullishSL = support - atr * atrMultiplierSL
bullishTP1 = support + atr * rewardToRisk * atrMultiplierTP1
bullishTP2 = support + atr * rewardToRisk * atrMultiplierTP2

bearishSL = resistance + atr * atrMultiplierSL
bearishTP1 = resistance - atr * rewardToRisk * atrMultiplierTP1
bearishTP2 = resistance - atr * rewardToRisk * atrMultiplierTP2

// Visualization for Bullish Scenario (TP1, TP2, SL Lines with Labels)
var line bullishTP1Line = na
var line bullishTP2Line = na
var line bullishSLLine = na
var label bullishTP1Label = na
var label bullishTP2Label = na
var label bullishSLLabel = na

if isBullishEntry
    if na(bullishTP1Line)
        bullishTP1Line := line.new(bar_index - 10, bullishTP1, bar_index + 10, bullishTP1, color=color.green, width=2)
    else
        line.set_xy1(bullishTP1Line, bar_index - 10, bullishTP1)
        line.set_xy2(bullishTP1Line, bar_index + 10, bullishTP1)
    
    if na(bullishTP1Label)
        bullishTP1Label := label.new(bar_index + 10, bullishTP1, "TP1", color=color.green, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bullishTP1Label, bar_index + 10, bullishTP1)

    if na(bullishTP2Line)
        bullishTP2Line := line.new(bar_index - 10, bullishTP2, bar_index + 10, bullishTP2, color=color.green, width=2)
    else
        line.set_xy1(bullishTP2Line, bar_index - 10, bullishTP2)
        line.set_xy2(bullishTP2Line, bar_index + 10, bullishTP2)

    if na(bullishTP2Label)
        bullishTP2Label := label.new(bar_index + 10, bullishTP2, "TP2", color=color.green, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bullishTP2Label, bar_index + 10, bullishTP2)

    if na(bullishSLLine)
        bullishSLLine := line.new(bar_index - 10, bullishSL, bar_index + 10, bullishSL, color=color.red, width=2)
    else
        line.set_xy1(bullishSLLine, bar_index - 10, bullishSL)
        line.set_xy2(bullishSLLine, bar_index + 10, bullishSL)

    if na(bullishSLLabel)
        bullishSLLabel := label.new(bar_index + 10, bullishSL, "SL", color=color.red, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bullishSLLabel, bar_index + 10, bullishSL)

// Visualization for Bearish Scenario (TP1, TP2, SL Lines with Labels)
var line bearishTP1Line = na
var line bearishTP2Line = na
var line bearishSLLine = na
var label bearishTP1Label = na
var label bearishTP2Label = na
var label bearishSLLabel = na

if isBearishEntry
    if na(bearishTP1Line)
        bearishTP1Line := line.new(bar_index - 10, bearishTP1, bar_index + 10, bearishTP1, color=color.red, width=2)
    else
        line.set_xy1(bearishTP1Line, bar_index - 10, bearishTP1)
        line.set_xy2(bearishTP1Line, bar_index + 10, bearishTP1)
    
    if na(bearishTP1Label)
        bearishTP1Label := label.new(bar_index + 10, bearishTP1, "TP1", color=color.red, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bearishTP1Label, bar_index + 10, bearishTP1)

    if na(bearishTP2Line)
        bearishTP2Line := line.new(bar_index - 10, bearishTP2, bar_index + 10, bearishTP2, color=color.red, width=2)
    else
        line.set_xy1(bearishTP2Line, bar_index - 10, bearishTP2)
        line.set_xy2(bearishTP2Line, bar_index + 10, bearishTP2)

    if na(bearishTP2Label)
        bearishTP2Label := label.new(bar_index + 10, bearishTP2, "TP2", color=color.red, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bearishTP2Label, bar_index + 10, bearishTP2)

    if na(bearishSLLine)
        bearishSLLine := line.new(bar_index - 10, bearishSL, bar_index + 10, bearishSL, color=color.green, width=2)
    else
        line.set_xy1(bearishSLLine, bar_index - 10, bearishSL)
        line.set_xy2(bearishSLLine, bar_index + 10, bearishSL)

    if na(bearishSLLabel)
        bearishSLLabel := label.new(bar_index + 10, bearishSL, "SL", color=color.green, textcolor=color.white, yloc=yloc.price, style=label.style_label_right)
    else
        label.set_xy(bearishSLLabel, bar_index + 10, bearishSL)
